package org.colcon.web.model;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import org.colcon.web.model.framework.IItem;
import org.colcon.web.model.parsers.CLIParser;

//import com.google.gwt.i18n.shared.DateTimeFormat;

public class Item implements IItem
{
	public String getType(){return "item";}
	
	private static final long serialVersionUID = 7553021518413774081L;
	
	protected static final String
	
		// arguments
		TYPE = "type",
		PARENT = "parent",
		ID = "id";
	
	private Date date;
	
	private Map<String, String> args;
	
	public Item()
	{
		this(new LinkedHashMap<String, String>());
	}
	
	public Item(Map<String, String> args)
	{
		this(args.get(PARENT), args.get(ID));
	}
	
	public Item(String parent)
	{
		this(parent, null);
	}
	
	public Item(String parent, String id)
	{
		date = new Date();
		args = new LinkedHashMap<String, String>();
		
		setArg(PARENT, parent);
		setArg(TYPE, getType());
		setArg(ID, id);
	}

	public String getText()
	{
		return toString();
	}
	
	public String getArg(String key)
	{
		return args.get(key);
	}
	
	public boolean hasArg(String key)
	{
		return args.containsKey(key) && args.get(key) != null;
	}
	
	public void setArg(String key, String value)
	{
		args.put(key, value);
	}
	
	public String getId()
	{
		return getArg(ID);
	}
	
	public void setId(String id)
	{
		setArg(ID, id);
	}

	public String getParentPath()
	{
		return getArg(PARENT);
	}
	
	public void setParentPath(String parent)
	{
		setArg(PARENT, parent);
	}
	
	public String getPath()
	{
		if(getParentPath() == null)
			return getId();
		
		return getParentPath() + '/' + getId();
	}
	
	public Date getDate()
	{
		return date;
	}
	
	public void setDate(Date date)
	{
		this.date = date;
	}
	
	public String getDateString()
	{
		return date.toString();
	}
	
	public boolean isRoot()
	{
		return getParentPath() == null;
	}
	
	public int compareTo(IItem item)
	{
		return -getId().compareTo(item.getId());
	}
	
	public String toString()
	{
		String str = "";
		
		for(String key : args.keySet())
		{
			String value = args.get(key);
			
			if(value == null)
				continue;
			
			if(value.split(" ").length > 1)
				value = '{' + value + '}';
			
			str += '-' + key + ' ' + value + ' ';
		}
		
		str = str.trim();
		
		return str;
	}
	
	public Map<String, String> toMap()
	{
		return CLIParser.parse(toString());
	}
	
	public static String pathToId(String path)
	{
		if(path == null || path.length() == 0)
			return null;
		
		String[] array = path.split("/");
		
		if(array == null || array.length == 0)
			return null;
		
		return array[array.length - 1];
	}
	
	public static String pathToParent(String path)
	{
		if(path == null || path.length() <= 1)
			return null;
		
		String[] array = path.split("/");
		
		if(array == null || array.length <= 1)
			return null;
		
		String parent = array[0];
		
		for(int i = 1; i < array.length - 1; i++)
			parent += "/" + array[i];
		
		return parent;
	}
	
	public static String pathToGrandParent(String path)
	{
		return pathToParent(pathToParent(path));
	}
}
